﻿#region license
// CopyRight (c) 2009-2010 HangJing Teconology. All Rights Reserved.
// Function :礼品信息详细
// Created by tuhui at 2010-6-24 16:28:14.
// E-Mail: tuhui@ihangjing.com
#endregion
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Hangjing.SQLServerDAL;
using Hangjing.Model;
using Hangjing.Common;
using Hangjing.DBUtility;
using Hangjing.WebCommon;
using System.Collections.Generic;

public partial class qy_55tuan_Admin_DeliverDetail : System.Web.UI.Page
{
    Deliver dal = new Deliver();
    private string SqlWhere
    {
        get
        {
            object o = ViewState["SqlWhere"];
            return (o == null) ? string.Empty : o.ToString();
        }
        set
        {
            ViewState["SqlWhere"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ValidatorSet validator = new ValidatorSet("Admin");
        validator.SetValidator();
        if (!Page.IsPostBack)
        {
            int cityid = HjNetHelper.GetQueryInt("cityid", 0);
            WebUtility.BindList("id", "classname", SectionProxyData.GetEdelivergroupList().Where(a=>a.Status == cityid).ToList(), ddlDeliverGroup);
            if (HjNetHelper.GetQueryString("id") == "")
            {
                pageType.Text = "添加配送员";
                diagram_tab.Style["display"] = "none";
                sumbox.Style["display"] = "none";
                rblrevevar3.Items[0].Selected = true;


                EAdminInfo admin = UserHelp.GetAdmin();
                if (cityid == 0 && admin.CityID > 0)
                {
                    Response.Redirect("DeliverDetail.aspx?cityid=" + admin.CityID);
                }
                if (cityid == 0)
                {
                    return;
                }


            }
            else
            {
                pageType.Text = "编辑配送员";
                //控件绑定内容
                DeliverInfo info = dal.GetModel(Convert.ToInt32(HjNetHelper.GetQueryString("id")));
                if (info != null)
                {
                    this.tbName.Text = info.Name;
                    this.tbPhone.Text = info.Phone;
                    hidPassword.Value = info.Password;
                    tbUserName.Text = info.UserName;
                    WebUtility.SelectValue(ddlDeliverGroup, info.GpsIMEI);

                    WebUtility.SelectValue(tbworkstate, info.workstate);
                    if (info.workstate != 40)
                    {
                        tbworkstate.Enabled = false;
                    }

                    WebUtility.SelectValue(tbcartype, info.cartype);
                    WebUtility.SelectValue(tbcarower, info.carower);
                    WebUtility.SelectValue(tbpost, info.post);
                    WebUtility.SelectValue(tbgrade, info.grade);
                    tbcarnum.Text = info.carnum;
                    tbrevefloat1.Text = info.revefloat1.ToString();

                    string sumsql = "orderstatus = 3 and deliverid = "+info.DataId;

                    CustorderInfo count = new Custorder().GetCountAndTotal1(sumsql);

                    lborder.InnerText = count.OrderCount.ToString();
                    lballdistance.InnerText = count.cardpay.ToString();
                    WebUtility.SelectValue(ddreveint1, info.reveint1);
                    tbreveint2.Text = info.reveint2.ToString();
                    tbreveint3.Text = info.reveint3.ToString();

                    IList<GPS_RecordsInfo> gpslist = new GPS_Records().GetList(1, 1, "jh1=" + info.DataId, "id", 1);
                    foreach (var item in gpslist)
                    {
                        if (item.AddTime > DateTime.Now.AddMinutes(-1*Hangjing.Common.Constant.OffLineDoor))
                        {
                            lbstatus.Text = "在线";
                        }
                    }

                    WebUtility.SelectValue(ddlSection, info.Section);
                    tbInve2.Text = info.Inve2;
                    tbrevefloat2.Text = info.revefloat2.ToString();

                    WebUtility.SelectValue(ddlrevevar1, info.revevar1);
                    WebUtility.SelectValue(ddlrevevar2, info.revevar2);
                    WebUtility.SelectValue(rblrevevar3, info.revevar3);
                    lbmoney.InnerText = info.usermoney.ToString();
                }
            }
        }
    }

    protected void btSave_Click(object sender, EventArgs e)
    {
        OperateLogManager<DeliverInfo> logmanager = new OperateLogManager<DeliverInfo>(UserHelp.GetAdmin().AdminName, TableENUM.Deliver.ToString());

        DeliverInfo oldmodel = null;
        DeliverInfo info = new DeliverInfo();
        info.DataId = HjNetHelper.GetQueryInt("id", 0);
        if (info.DataId > 0)
        {
            info=  dal.GetModel(info.DataId);
            oldmodel = logmanager.Clone(info);
        }
        else
        {
            info.CodeId = "0";
            info.Status = 0;
            info.OrderNum = 0;
            info.usermoney = 0;
            info.AddDate = DateTime.Now;
        }

        info.Section = ddlSection.SelectedValue;
        info.Name = WebUtility.InputText(this.tbName.Text);
        info.Phone = WebUtility.InputText(this.tbPhone.Text);
        info.GpsIMEI = WebUtility.InputText(this.ddlDeliverGroup.Text);

        info.UserName = WebUtility.InputText(tbUserName.Text);
        info.Password = !string.IsNullOrEmpty(this.tbpassword.Text) ? WebUtility.GetMd5(this.tbpassword.Text) : hidPassword.Value;
        info.Inve2 = WebUtility.InputText(tbInve2.Text);
        int cityid = HjNetHelper.GetQueryInt("cityid", 0);
        info.Inve1 = cityid;
       
        info.workstate = WebUtility.InputText(tbworkstate.SelectedValue, true);
        info.cartype = WebUtility.InputText(tbcartype.SelectedValue);
        info.carower = WebUtility.InputText(tbcarower.SelectedValue, true);
        info.carnum = WebUtility.InputText(tbcarnum.Text);
        info.post = WebUtility.InputText(tbpost.SelectedValue, true);
        info.grade = WebUtility.InputText(tbgrade.SelectedValue,true);
        info.reveint1 = WebUtility.InputText(this.ddreveint1.SelectedValue, true);
        info.reveint2 = WebUtility.InputText(this.tbreveint2.Text, true);
        info.reveint3 = WebUtility.InputText(this.tbreveint3.Text, 'c');
        info.revevar1 = ddlrevevar1.SelectedValue; ;
        info.revevar2 = ddlrevevar2.SelectedValue; ;
        info.revevar3 = rblrevevar3.SelectedValue;
        info.revefloat1 = WebUtility.InputText(tbrevefloat1.Text, 'c');
        info.revefloat2 =WebUtility.InputText(tbrevefloat2.Text, 'c');
        info.revedate = Convert.ToDateTime("1970-1-1");


        info.reveint4 = 0;
        info.reveint5 = 0;
        info.reveint6 = 0;
        info.revevar4 = "";
        info.revevar5 = "";
        info.revefloat3 = 0;
        info.revefloat4 = 0;


        if (info.DataId == 0)
        {
            //判断权限
            int _rs = WebUtility.checkOperator(2);
            if (_rs == 0)
            {
                AlertScript.RegScript(this.Page, this.UpdatePanel1, "alert('无操作权限','success','true',5);");
                return;
            }

            string sqls = "Name='" + info.Name + "' and delflag = 0 ";
            if (dal.GetCount(sqls) > 0)
            {
                AlertScript.RegScript(this.Page, UpdatePanel1, "alert('相同名称的配送员已经存在','text:新增商家信息失败，登录名已存在!','250','150','true','3000','true','text');init();");
                return;
            }


            sqls = "UserName='" + info.UserName + "' and delflag = 0 ";
            if (dal.GetCount(sqls) > 0)
            {
                AlertScript.RegScript(this.Page, UpdatePanel1, "alert('相同账号的配送员已经存在','text:新增商家信息失败，登录名已存在!','250','150','true','3000','true','text');init();");
                return;
            }


            //操作日志
            deliverlogInfo item = new deliverlogInfo();
            item.admin = logmanager.adminname;
            item.optime = DateTime.Now;
            item.eventname = "添加";
            item.columntext = info.Name;
            item.columnname = "Name";
            item.oldvalue = "";
            item.newvalue = "";
            item.reveint1 = info.Inve1;
            item.reveint2 = 0;
            item.revevar1 = info.Name;
            item.revevar2 = TableENUM.Deliver.ToString();
            new deliverlog().Add(item);



            if (dal.Add(info) > 0)
            {
                AlertScript.RegScript(this.Page, UpdatePanel1, "showMessage('新增成功','success','true',5);");
            }
            else
            {
                AlertScript.RegScript(this.Page, this.UpdatePanel1, "showMessage('新增失败','error','true',5);");
            }

        }
        else
        {

            //判断权限
            int _rs = WebUtility.checkOperator(3);
            if (_rs == 0)
            {
                AlertScript.RegScript(this.Page, this.UpdatePanel1, "alert('无操作权限','success','true',5);");
                return;
            }

            string sqls = "Name='" + info.Name + "' and delflag = 0 and dataid <> "+info.DataId;
            if (dal.GetCount(sqls) > 0)
            {
                AlertScript.RegScript(this.Page, UpdatePanel1, "alert('相同名称的配送员已经存在','text:新增商家信息失败，登录名已存在!','250','150','true','3000','true','text');init();");
                return;
            }

            sqls = "UserName='" + info.UserName + "' and delflag = 0 and dataid <> " + info.DataId;
            if (dal.GetCount(sqls) > 0)
            {
                AlertScript.RegScript(this.Page, UpdatePanel1, "alert('相同账号的配送员已经存在','text:新增商家信息失败，登录名已存在!','250','150','true','3000','true','text');init();");
                return;
            }



            if (dal.Update(info) > 0)
            {
                IList<deliverlogInfo> logs = logmanager.Compare(oldmodel, info,info.Name);
                if (logs.Count > 0)
                {
                    foreach (var item in logs)
                    {
                        item.reveint1 = info.Inve1;
                    }

                     DataTable dt = CollectionHelper.ConvertTo(logs, "deliverlog");
                     SQLHelper.SqlBulkCopyData(dt);
               
                }

                AlertScript.RegScript(this.Page, this.UpdatePanel1, "showMessage('编辑成功','success','true',5);");
            }
            else
            {
                AlertScript.RegScript(this.Page, this.UpdatePanel1, "showMessage('编辑失败','error','true',5);");
            }
        }

    }
}
